package com.ttai.model.dao;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

import com.ttai.untils.Constant;

public  class DBHelper extends SQLiteOpenHelper {

    // 创建用户表的ＳＱＬ语句
//    public static final String CREATE_TABLE_USER = "create table newacount ("
//            + "acountname text primary key,"
//            + "imgurl text,"
//            + "phone text,"
//            + "acounttip text)";
//    public static final String CREATE_TABLE_TIP = "create table tipacount("
//            + "phone text primary key,"
//            + "tip text)";
//
//
    public static final String CREATE_TABLE_APP = "create table appacount("
            + "appid text primary key,"
            + "appname text)";
//
//    public static final String CREATE_TABLE_IDACOUNT ="create table idacount("
//            + "id integer primary key autoincrement,"
//            + "phone text,"
//            + "appname text,"
//            + "imgurl text,"
//            + "appid text,"
//            + "acountnumber text,"
//            + "acountnumberlogo text)";

    public static final String CREATE_TABLE_TOKEN = "create table tokenacount("
            + "id integer primary key autoincrement,"
            + "token text)";

    public static final String CREATE_TABLE_CKEY = "create table ckeyacount("
            + "id integer primary key autoincrement,"
            + "ckey text)";

    public static final String INSERT_TABLE = "insert into tokenacount values(1,1)";
    public static final String INSERT_CKEY = "insert into ckeyacount values(1,1)";
    private static final String DBNAME = "UserManage.db";

    private Context context;


    public DBHelper(Context context) {
        super(context,DBNAME, null, Constant.VERSION);
        // TODO Auto-generated constructor stub
        this.context=context;
    }

    private static volatile DBHelper instance;

    /**
     * 单例模式
     * 双重校验锁
     * @param context
     * @return
     */
    public static DBHelper getInstance(Context context){
        if (instance == null){
            synchronized (DBHelper.class){
                if (instance == null){
                    instance = new DBHelper(context);
                }
            }
        }
        return instance;
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
        // 原生建表的做法
//        db.execSQL(CREATE_TABLE_USER);
        db.execSQL(CREATE_TABLE_APP);
//        db.execSQL(CREATE_TABLE_TIP);
//        db.execSQL(CREATE_TABLE_IDACOUNT);
        db.execSQL(CREATE_TABLE_TOKEN);
        db.execSQL(CREATE_TABLE_CKEY);
        db.execSQL(INSERT_TABLE);
        db.execSQL(INSERT_CKEY);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int arg1, int arg2) {
        // TODO Auto-generated method stub
//        db.execSQL("drop table if exists newacount");
//        db.execSQL("drop table if exists tipacount");
//        db.execSQL("drop table if exists idacount");
        db.execSQL("drop table if exists appacount");
        db.execSQL("drop table if exists tokenacount");
        db.execSQL("drop table if exists ckeyacount");
        onCreate(db);
    }
}